import { modal } from 'vue-strap'
import extend from 'extend'
import { showMsg, chargeDataflow } from '../../../../vuex/action'

const validation = {
  ac(data) {
    for (let name in data) {
      if ((data[name] + '').length === 0) {
        return '* 标记的均为必填/必选项'
      }
    }
    return true
  }
}

export default {
  name: 'charge-flow',
  components: {
    'vue-modal': modal
  },
  props: ['showCharge', 'item', 'title'],
  data() {
    return {
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      XChargeDataflow: chargeDataflow
    }
  },
  ready() {

  },
  methods: {
    submit() {
      var item = extend(true, {}, this.item)
      var valid = validation.ac(item)
      if (valid !== true) {
        return this.xShowMsg({
          type: 'danger',
          content: valid
        })
      }
      this.XChargeDataflow({activityId: item.activityId, phone: item.phone, data: item.data})
        .then((data) => {
          this.showCharge = false
          this.xShowMsg({
            type: 'info',
            content: data.result || '下单成功'
          })
        })
        .catch((res) => {
          this.showCharge = false
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg || '下单失败'
          })
        })
    }
  }
}
